﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HomeCare.Control;
using HomeCare.Model;

namespace HomeCare.View
{
    public partial class CadAcesso : Form
    {
        public CadAcesso()
        {
            InitializeComponent();
        }

        Bitmap imgInclusaoAtivo = (Bitmap)Properties.Resources._001_ATIVO;
        Bitmap imgAlteracaoAtivo = (Bitmap)Properties.Resources._004_ATIVO;
        Bitmap imgExclusaoAtivo = (Bitmap)Properties.Resources._002_ATIVO;
        Bitmap imgConsultaAtivo = (Bitmap)Properties.Resources._003_ATIVO;
        Bitmap imgExclusaoDesativo = (Bitmap)Properties.Resources._002_DESATIVO;
        Bitmap imgAlteracaoDesativo = (Bitmap)Properties.Resources._004_DESATIVO;
        Bitmap imgInclusaoDesativo = (Bitmap)Properties.Resources._001_DESATIVO;
        Bitmap imgConsultaDesativo = (Bitmap)Properties.Resources._003_DESATIVO;

        private void CadAcesso_Load(object sender, EventArgs e)
        {
            gdvAcessos.MultiSelect = false;
            gdvAcessos.Columns.AddRange(
            new DataGridViewTextBoxColumn(),
            new DataGridViewImageColumn(),
            new DataGridViewImageColumn(),
            new DataGridViewImageColumn(),
            new DataGridViewImageColumn());
            gdvAcessos.Columns[0].Name = "Descricao";
            gdvAcessos.Columns[1].Name = "Inclusao";
            gdvAcessos.Columns[2].Name = "Alteracao";
            gdvAcessos.Columns[3].Name = "Exclusao";
            gdvAcessos.Columns[4].Name = "Consulta";

            gdvAcessos.Columns[0].Width = 250;
            gdvAcessos.Columns[1].Width = 30;
            gdvAcessos.Columns[2].Width = 30;
            gdvAcessos.Columns[3].Width = 30;
            gdvAcessos.Columns[4].Width = 30;

            Perfil perfil = new Perfil();
            PerfilControl perfilControl = new PerfilControl();
            DataSet dtsPerfil = perfilControl.ConsultaPerfil(perfil);
            
            
            cbxPerfil.DisplayMember = dtsPerfil.Tables[0].Columns["Perfil"].ToString();
            cbxPerfil.ValueMember = dtsPerfil.Tables[0].Columns["ID"].ToString();
            cbxPerfil.DataSource = dtsPerfil.Tables[0];

            
        }

        private void cbxPerfil_SelectedIndexChanged(object sender, EventArgs e) {
            gdvAcessos.Rows.Clear();
            TelaControl telaControl = new TelaControl();
            Perfil perfil = new Perfil();
            perfil.id = Int32.Parse(cbxPerfil.SelectedValue.ToString());
            DataSet dtsAcessso = telaControl.ListaAcesso(perfil);
            //gdvAcessos.DataSource = dtsAcessso.Tables[0];

            for (int i = 0; i < dtsAcessso.Tables[0].Rows.Count; i++) {
                gdvAcessos.Rows.Add();
                gdvAcessos.Rows[i].Cells["Descricao"].Value = dtsAcessso.Tables[0].Rows[i].ItemArray[0].ToString();
                gdvAcessos.Rows[i].Cells["Inclusao"].Value = dtsAcessso.Tables[0].Rows[i].ItemArray[1].ToString();
                gdvAcessos.Rows[i].Cells["Alteracao"].Value = dtsAcessso.Tables[0].Rows[i].ItemArray[2].ToString();
                gdvAcessos.Rows[i].Cells["Exclusao"].Value = dtsAcessso.Tables[0].Rows[i].ItemArray[3].ToString();
                gdvAcessos.Rows[i].Cells["Consulta"].Value = dtsAcessso.Tables[0].Rows[i].ItemArray[4].ToString();
            }

            gdvAcessos.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.gdvAcessos_CellFormating);
            //this.Controls.Add(gdvAcessos);
            //cbxPerfil.SelectedIndex = 0;
        }


        private void gdvAcessos_CellFormating(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            // Replace string values in the Priority column with images.
            if (gdvAcessos.Columns[e.ColumnIndex].Name.Equals("Inclusao"))
            {
                // Ensure that the value is a string.
                String stringValue1 = e.Value as string;
                if (stringValue1 == null) return;

                // Set the cell ToolTip to the text value.
                DataGridViewCell cell1 = gdvAcessos[e.ColumnIndex, e.RowIndex];
                cell1.ToolTipText = stringValue1;

                // Replace the string value with the image value.
                switch (stringValue1)
                {
                    case "S":
                        e.Value = imgInclusaoAtivo;
                        break;
                    case "N":
                        e.Value = imgInclusaoDesativo;
                        break;
                }
            }

            if (gdvAcessos.Columns[e.ColumnIndex].Name.Equals("Alteracao"))
            {
                // Ensure that the value is a string.
                String stringValue2 = e.Value as string;
                if (stringValue2 == null) return;

                // Set the cell ToolTip to the text value.
                DataGridViewCell cell2 = gdvAcessos[e.ColumnIndex, e.RowIndex];
                cell2.ToolTipText = stringValue2;

                // Replace the string value with the image value.
                switch (stringValue2)
                {
                    case "S":
                        e.Value = imgAlteracaoAtivo;
                        break;
                    case "N":
                        e.Value = imgAlteracaoDesativo;
                        break;
                }
            }


            if (gdvAcessos.Columns[e.ColumnIndex].Name.Equals("Exclusao"))
            {
                // Ensure that the value is a string.
                String stringValue3 = e.Value as string;
                if (stringValue3 == null) return;

                // Set the cell ToolTip to the text value.
                DataGridViewCell cell3 = gdvAcessos[e.ColumnIndex, e.RowIndex];
                cell3.ToolTipText = stringValue3;

                // Replace the string value with the image value.
                switch (stringValue3)
                {
                    case "S":
                        e.Value = imgExclusaoAtivo;
                        break;
                    case "N":
                        e.Value = imgExclusaoDesativo;
                        break;
                }
            }


            if (gdvAcessos.Columns[e.ColumnIndex].Name.Equals("Consulta"))
            {
                // Ensure that the value is a string.
                String stringValue4 = e.Value as string;
                if (stringValue4 == null) return;

                // Set the cell ToolTip to the text value.
                DataGridViewCell cell4 = gdvAcessos[e.ColumnIndex, e.RowIndex];
                cell4.ToolTipText = stringValue4;

                // Replace the string value with the image value.
                switch (stringValue4)
                {
                    case "S":
                        e.Value = imgConsultaAtivo;
                        break;
                    case "N":
                        e.Value = imgConsultaDesativo;
                        break;

                }
            }
        }
    }
}
